package com.top.ruizhong.popup

import android.content.Context
import android.graphics.Typeface
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.*
import androidx.recyclerview.widget.PagerSnapHelper
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.top.ruizhong.R
import com.top.ruizhong.entity.VideoCateList
import kotlinx.android.synthetic.main.activity_test_video.*
import kotlinx.android.synthetic.main.popup_dialog_picker.view.*
import razerdp.basepopup.BasePopupWindow


class VideoPickerPopup(
    context: Context, val onConfirm: (action: Action?, position: Int) -> Unit
) : BasePopupWindow(context) {

    init {
        popupGravity = Gravity.BOTTOM
        setOverlayNavigationBar(false)
    }

    private val pickerAdapter =
        object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_pupop_dialog_picker) {

            var selectedPosition = 0
                set(value) {
                    if (field != value) {
                        field = value
                        notifyDataSetChanged()
                    }
                }

            override fun convert(holder: BaseViewHolder, item: String) {
                holder.getView<TextView>(R.id.tvPopupDialogText).apply {
                    typeface = if (holder.layoutPosition == selectedPosition) {
                        Typeface.defaultFromStyle(Typeface.BOLD)
                    } else {
                        Typeface.defaultFromStyle(Typeface.NORMAL)
                    }
                    textSize = if (holder.layoutPosition == selectedPosition)
                        18f else 15f
                    setTextColor(
                        if (holder.layoutPosition == selectedPosition) ColorUtils.getColor(
                            R.color.colorMain
                        ) else ColorUtils.getColor(R.color.FF_AFAFAF)
                    )
                    text = item
                }
            }
        }

    private var mAction: Action? = null

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_dialog_picker)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)

        contentView.tvDialogPickerCancel.setOnClickListener {
            dismiss()
        }
        contentView.tvDialogPickerComplete.setOnClickListener {
            onConfirm(mAction, pickerAdapter.selectedPosition)
            dismiss()
        }
//        contentView.rvPopupDialogPicker.adapter = pickerAdapter
    }

    override fun onCreateShowAnimation(width: Int, height: Int): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f,
            Animation.RELATIVE_TO_PARENT, 0f
        ).apply { duration = 300L }
    }

    override fun onCreateDismissAnimation(): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f
        ).apply { duration = 300L }
    }


    fun setFillData(
        title: String, pickers: MutableList<String>
    ): VideoPickerPopup {
        contentView.tvPopupDialogTitle.text = title
        pickerAdapter.selectedPosition = 0
        pickerAdapter.setNewInstance(pickers)
        return this
    }

    fun setAction(action: Action): VideoPickerPopup {
        mAction = action
        return this
    }

    override fun showPopupWindow() {
        super.showPopupWindow()
        contentView.rvPopupDialogPicker.adapter =
            pickerAdapter.apply { setEmptyView(createLoadingView()) }
        pickerAdapter.setOnItemClickListener { _, _, position ->
            pickerAdapter.selectedPosition = position
        }
    }

    private fun createLoadingView(): View = LinearLayout(context).apply {
        val progressBar = ProgressBar(context)
        progressBar.layoutParams = LinearLayout.LayoutParams(
            ConvertUtils.dp2px(25f), ConvertUtils.dp2px(25f)
        )
        gravity = Gravity.CENTER
        addView(progressBar)
    }

    enum class Action {
        PICKER_VIDEO_KIND, PICKER_VIDEO_TYPE, PICKER_VIDEO_DRAFT
    }

}